package com.pengtu.entity.detect;

import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Where;

import com.pengtu.entity.IdEntity;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.utils.MathsUtils;

/**
 * 监督抽检数据
 * Spotreport.
 */
@Entity
@Table(name = "S_Q_DETECT_SPOTREPORT")
public class SpotReport extends IdEntity {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private ProjectInfo projectInfo;// 工程
	private String signid;// 合同段
	private String locale;// 镇区
	private String reporter;// 统计人
	private Date reportdate;// 上报时间
	private String batch;// 批次
	private String remark;// 备注
	private StructureSpot structurespot;
	private SurfaceSpot surfacespot;
	private TunnelSpot tunnelspot;
	private Bridgespot bridgespot;
	private Materialspot materialspot;
	private Safetyspot safetyspot;
	private PileSpot pilespot;
	
	private String replace;
	
	private Set<Attachment> attachments =  new HashSet<Attachment>(0); // 附件
	
	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public PileSpot getPilespot() {
		return pilespot;
	}

	public void setPilespot(PileSpot pilespot) {
		this.pilespot = pilespot;
	}

	public SpotReport() {
	}

	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public StructureSpot getStructurespot() {
		return structurespot;
	}

	public void setStructurespot(StructureSpot structurespot) {
		this.structurespot = structurespot;
	}

	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public SurfaceSpot getSurfacespot() {
		return surfacespot;
	}

	public void setSurfacespot(SurfaceSpot surfacespot) {
		this.surfacespot = surfacespot;
	}

	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public TunnelSpot getTunnelspot() {
		return tunnelspot;
	}

	public void setTunnelspot(TunnelSpot tunnelspot) {
		this.tunnelspot = tunnelspot;
	}

	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public Bridgespot getBridgespot() {
		return bridgespot;
	}

	public void setBridgespot(Bridgespot bridgespot) {
		this.bridgespot = bridgespot;
	}

	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public Materialspot getMaterialspot() {
		return materialspot;
	}

	public void setMaterialspot(Materialspot materialspot) {
		this.materialspot = materialspot;
	}

	@OneToOne(mappedBy = "spotReport", cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	public Safetyspot getSafetyspot() {
		return safetyspot;
	}

	public void setSafetyspot(Safetyspot safetyspot) {
		this.safetyspot = safetyspot;
	}

	@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST })
	@JoinColumn(name = "PID")
	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}

	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}

	@Column(name = "SIGNID", length = 48)
	public String getSignid() {
		return this.signid;
	}

	public void setSignid(String signid) {
		this.signid = signid;
	}

	@Column(name = "LOCALE", length = 100)
	public String getLocale() {
		return this.locale;
	}

	public void setLocale(String locale) {
		this.locale = locale;
	}

	@Column(name = "REPORTER", length = 100)
	public String getReporter() {
		return this.reporter;
	}

	public void setReporter(String reporter) {
		this.reporter = reporter;
	}

	@Temporal(TemporalType.DATE)
	@Column(name = "REPORTDATE", length = 7)
	public Date getReportdate() {
		return this.reportdate;
	}

	public void setReportdate(Date reportdate) {
		this.reportdate = reportdate;
	}

	@Column(name = "BATCH", length = 200)
	public String getBatch() {
		return this.batch;
	}

	public void setBatch(String batch) {
		this.batch = batch;
	}

	@Column(name = "REMARK", length = 400)
	public String getRemark() {
		return this.remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@Transient
	public double getAllCount(){
		double a = 0,b = 0,c = 0,d = 0,e = 0,f = 0;
		if(structurespot != null){ a = structurespot.getCountF200();}
		if(surfacespot != null){ b = surfacespot.getCountF300();}
		if(tunnelspot != null){ c = tunnelspot.getCountF500();}
		if(bridgespot != null){ d = bridgespot.getCountF400();}
		if(materialspot != null){ e = materialspot.getCountF900();}
		if(safetyspot != null){ f = safetyspot.getCountF600();}
		return MathsUtils.sum(a,b,c,d,e,f);
	}
	
	@Transient
	public double getAllPass(){
		double a = 0,b = 0,c = 0,d = 0,e = 0,f = 0;
		if(structurespot != null){ a = structurespot.getPassF200();}
		if(surfacespot != null){ b = surfacespot.getPassF300();}
		if(tunnelspot != null){ c = tunnelspot.getPassF500();}
		if(bridgespot != null){ d = bridgespot.getPassF400();}
		if(materialspot != null){ e = materialspot.getPassF900();}
		if(safetyspot != null){ f = safetyspot.getPassF600();}
		return MathsUtils.sum(a,b,c,d,e,f);
	}

	@Transient
	public double getAllTotalPassrate() {
		if(getAllCount()==0){
			return 0;}
		else {
		return MathsUtils.div("2",getAllPass(),getAllCount(), 0.01);
	}
	}
	
	@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinColumn(name = "TABLEID")
	@Where(clause=" delFlag = 0")
	public Set<Attachment> getAttachments() {
		return attachments;
	}
	public void setAttachments(Set<Attachment> attachments) {
		this.attachments = attachments;
	}

	/**
	 * 只是用来代替某个字符串的意义
	 *
	 * @return  the replace
	 * @since   1.0.0
	 */
	@Transient
	public String getReplace() {
		return replace;
	}

	/**只是用来代替某个字符串的意义
	 * @param replace the replace to set
	 */
	public void setReplace(String replace) {
		this.replace = replace;
	}


	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "SpotReport [projectInfo=" + projectInfo + ", locale=" + locale
				+ "]";
	}
	
}
